package cn.sfa.controller.manage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.WebInitBinderContoller;
import cn.sfa.entity.users.SaleUsersLogin;
import cn.sfa.service.log.LogService;

@Controller
@RequestMapping("manage/joblogs/index")
public class ManageJobLogsController extends WebInitBinderContoller {

	Logger log = LoggerFactory.getLogger(ManageJobLogsController.class);
	
	@Autowired
	private LogService jobService;

	public LogService getJobService() {
		return jobService;
	}

	public void setJobService(LogService jobService) {
		this.jobService = jobService;
	}

	// 判断是否是管理员
	public boolean isAdmin(HttpServletRequest request) {
		SaleUsersLogin saleUser = (SaleUsersLogin) request.getSession().getAttribute("user");
		if (saleUser.getUserType().equals("USERTYPE_3"))
			return true;
		return false;
	}

	/**
	 * 获取所有登录日志
	 * 
	 * @author: LiXiang
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getlogs")
	public String getLogs(
			@RequestParam(value = "fromDate", required = false) String fromDate,
			@RequestParam(value = "toDate", required = false) String toDate,
			HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		int currentPage = 1;
		
		if (!CommonUtil.isEmpty(request.getParameter("currentPage"))) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		}
		String url = request.getContextPath() + "/manage/joblogs/index/getlogs?fromDate="+fromDate+"&toDate="+toDate;
		Pagination pagination = jobService.getLogsByDate(currentPage, 10, url, fromDate, toDate);
		
		map.put("pagination", pagination);
		map.put("fromDate", fromDate);
		map.put("toDate", toDate);
		return "manage/joblogs/getlogs";
	}
	
	/**
	 * 获取日志详细
	 * 
	 * @author: LiXiang
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getlogsdetail")
	public String getLogsDetail(
			@RequestParam(value = "fromDate", required = false) String fromDate,
			HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		int currentPage = 1;
		
		if (!CommonUtil.isEmpty(request.getParameter("currentPage"))) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		}
		fromDate = fromDate.replaceAll("-", "");
		String url = request.getContextPath() + "/manage/joblogs/index/getlogsdetail?fromDate="+fromDate;
		Pagination pagination = jobService.getLogsDetail(currentPage, 10, url, fromDate);
		
		map.put("pagination", pagination);
		map.put("fromDate", fromDate);
		return "manage/joblogs/getlogsdetail";
	}
	
}
